% Endogenous Firm Competition and Cyclicality of Markups, Customer Base Model
% Hassan Afrouzi
% November 2015


%----------------------------------------------------------------
% 1. Defining variables
%----------------------------------------------------------------

var y c k x l lambda nu r w mu;         %y is output, c is consumption, k is capital, x is investment, l is labor,
                                        %lambda is marginal utility of consumption, nu over lambda is price of investment
                                        %r is rate of return on capital, w is wages and mu is the average markup in the economy.
var q S Sp g dy z_a z_g;                %q is the stochastic discount factor, S is the function in the investment adjustment cost,
                                        %Sp is derivative of S, g is government spending, z_a is technology shock and z_g is the 
                                        %government spending shock
var psi_1 psi_2;
varexo e u;
 
parameters beta psi delta alpha gamma gss sigma theta phi N eta muc h_prime e_bar mu_bar zeta;
parameters a b rho_a rho_g_1 rho_g_2;
parameters var_e var_u;                 %parameters are explained below.

%----------------------------------------------------------------
% 2. Calibration
%----------------------------------------------------------------
load parameterfile;
set_param_value('a',a_p);               %Investment adjustment cost parameters
set_param_value('b',b_p);               %Habit persistence parameter
set_param_value('rho_g_1',rho_g_1_p);   %first lag coefficient in gov't spending shock
set_param_value('rho_g_2',rho_g_2_p);   %second lag coefficient in gov't spending shock
set_param_value('gamma',gamma_p);       %1-probability of renegotiation
set_param_value('N',N_p);               %Number of firms in each industry
set_param_value('var_e',var_e_p);       %variance of technology shock
set_param_value('var_u',var_u_p);       %variance of gov't spending shock
set_param_value('phi',phi_p);           %coefficient on disutility of labor
theta	= 2;                            %inverse of intertemporal elasticity of substitution
alpha   = 0.35;                         %share of capital in output
beta    = 0.993;                        %HH's discount factor
delta   = 0.025;                        %depreciation of capital
psi     = 0.4;                          %Frisch elasticity of labor
rho_a   = 0.95;                         %persistence of technology shock
sigma   = 4;                           %elasticity of substitution across industry goods
eta     = 10;                         %elasticity of substitution within industry goods
gss     = -1.6;                         %log(steady state government spending as share of output)
muc     = (eta*(N-1)+sigma)/(eta*(N-1)+sigma-N);    %Markup when everyone plays the static Nash equilibrium (punishment)
mu_bar  = 1.1;
e_bar   = ((N-1)*eta+sigma)/N;          %elasticity of demand for a single firm
zeta    = (1/mu_bar - 1/muc)*(1-beta*gamma)/(1-1/mu_bar);
h_prime = - e_bar*zeta/gamma;   %h'(1)

%----------------------------------------------------------------
% 3. Model: 
%    all variables are defined as logs of the corresponding 
%    variables in the model, except for function S(.) and 
%    its derivative, Sp.
%----------------------------------------------------------------

model; 

%--------------------
% 3.1. Definitions
%--------------------

% Defining cost of investment
    
    S=(a/2)*((exp(x-x(-1))-1)^2);

% Derivative of function S(.)

    Sp=a*(exp(x-x(-1))-1);

% Expected Growth rate of output

    dy=y(+1)-y;

% Stochastic Discount Factor

    q=log(beta)+lambda(+1)-lambda;


%--------------------
% 3.2. HHs
%--------------------

% FOC with respect to consumption
    
    exp(lambda)=(exp(c)-b*exp(c(-1)))^(-theta)-b*beta*(exp(c(+1))-b*exp(c))^(-theta);

% FOC with respect to labor

    phi*exp(l)^psi=exp(lambda)*exp(w);

% FOC with respect to investment
    
    exp(lambda)=exp(nu)*(1-S-(exp(x-x(-1)))*Sp)+beta*exp(nu(+1))*((exp(x(+1)-x))^2)*Sp(+1);

% FOC with respect to capital

    exp(nu)=beta*(exp(lambda(+1))*exp(r(+1))+exp(nu(+1))*(1-delta));

% Law of motion for capital

    (1-S)*exp(x)=exp(k)-exp(k(-1))*(1-delta);



%--------------------
% 3.3. Firms
%--------------------

% Production function
    
    y=z_a+alpha*k(-1)+(1-alpha)*l;

% Cost minimization FOC for capital

    r=-mu+log(alpha)+y-k(-1);

% Cost minimization FOC for labor

    w=-mu+log(1-alpha)+y-l;

% First order condition of the firm

% (1+h_prime)*(exp(-mu)-1/muc)-h_prime*(1-1/muc)=gamma*exp(y(+1)-y+q)*(exp(-mu(+1))-1/muc);
exp(-mu)-1/muc = (zeta/(1+zeta))*(1-1/muc) + beta*gamma*exp(y(+1)-y+q)*(exp(-mu(+1))-1/muc)/(1+zeta);

psi_1 = -beta*gamma*zeta*(1-1/muc)/((1+zeta)*((1-beta*gamma)/muc+zeta));
psi_2 = beta*gamma/(1+zeta);

%--------------------
% 3.4. Mkt Clearing & Gov't
%--------------------

exp(c)+exp(x)+exp(g)=exp(y);
g=gss+z_g;

%--------------------
% 3.5. Shock Processes
%--------------------

% Technology Shock

    z_a=rho_a*z_a(-1)+e;

% Gov't Spending Shock

    z_g=rho_g_1*z_g(-1)+rho_g_2*z_g(-2)+u;



end;

%----------------------------------------------------------------
% 4. Computation
%----------------------------------------------------------------

initval;
y       = log(1.01);
c       = log(0.56);
k       = log(8.4);
x       = log(0.2);
l       = log(0.33);
S       = 0;
Sp      = 0;
mu      = 0.3;
q       = 0;
y       = 0;

r       = log(0.03);
w       = log(1.63);
q       = log(beta);
z_a     = 0;
z_g     = 0;

end;

shocks;

var e = var_e;
var u = var_u;

end;

steady;

check;

stoch_simul(periods=10000,drop=1000,order = 1,noprint,nograph,irf=20);